package com.usian.admin.service.imlp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {

        if (null == channelDto) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        channelDto.checkParam ();

        Page<AdChannel> page = new Page<> (channelDto.getPage (), channelDto.getSize ());

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<> ();

        if (StringUtils.isNotBlank (channelDto.getName ())) {
            queryWrapper.like (AdChannel::getName, channelDto.getName ());
        }

        IPage<AdChannel> iPage = page (page, queryWrapper);

        PageResponseResult result = new PageResponseResult (channelDto.getPage (), channelDto.getSize (), (int) iPage.getTotal ());

        result.setData (iPage.getRecords ());

        return result;
    }


    @Override
    public ResponseResult insert(AdChannel adChannel) {

        if (null == adChannel) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        adChannel.setCreatedTime (new Date ());

        boolean save = this.save (adChannel);
        if (!save) {
            return ResponseResult.errorResult (AppHttpCodeEnum.SAVE_FAIL);
        }


        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {

        if (null == adChannel || null == adChannel.getId ()) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean updateById = this.updateById (adChannel);
        if (!updateById){
            return ResponseResult.errorResult (AppHttpCodeEnum.UPDATE_FAIL);
        }
        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteAdChannel(Integer id) {

        if (null == id){
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel adChannel = this.getById (id);

        if (null == adChannel){
            return ResponseResult.errorResult (AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (adChannel.getStatus ()){
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }

        boolean removeResult = this.removeById (id);

        if (!removeResult){
            return ResponseResult.errorResult (AppHttpCodeEnum.DELETE_FAIL);
        }
        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }
}
